// pages/mine_upBook/index.js
const getChina = require('../../../utils/region.js').getChina;
const apis = require('../../../utils/api').apis;
const api = require('../../../utils/api.js').apis;
const toDate = require('../../../utils/tool').toDate;
const oss = require('../../../utils/oss.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:null,
    name: "",
    level: "",
    imgUrl: "../../images/z-upbook.png",
    imgUrls:'',
  },
    // 图片上传未写完

    changeAvatar: function () {
      var that = this;
      wx.chooseImage({
        count: 1, //最多可以选择的图片总数
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function(res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          let tempFilePaths = res.tempFilePaths;
          api.getStsOss({}).then(res=>{
            // resolve(res)
            console.log(res)
            if (res.success) {
              // console.log(res.dadta.success)
              that.upLoadFileService(tempFilePaths, res.data)
            }
          })
          that.queryOssData().then(res => {
            console.log(res)
            
          })
        }
      });
    },
    // 更改图片路径
    upLoadFileService(file, ossData) {
      console.log(file,ossData)
      var arrurl = []
      const that = this;
      const date = new Date()
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      let obj = oss.util.aliOssParams(ossData.tempAk, ossData.tempSk,'https://xuedaozaixain.oss-cn-beijing.aliyuncs.com');
      let alikey = `user/${year}${month}${day}/user_head_${date.getTime()}_${Math.round(Math.random() * 100)}.${file[0].split('.').pop()}`;
      wx.uploadFile({
        url: 'https://xuedaozaixain.oss-cn-beijing.aliyuncs.com',
        filePath: file[0],
        name: 'file',
        formData: {
          key: alikey,
          name: file[0],
          policy: obj.policy,
          OSSAccessKeyId: ossData.tempAk,
          success_action_status: '200',
          signature: obj.signature,
          'x-oss-security-token': ossData.token
        },
        success: (res) => {
          console.log("https://xuedaozaixain.oss-cn-beijing.aliyuncs.com" + "/" + alikey)
  
          that.setData({
            imgUrls: 'https://xuedaozaixain.oss-cn-beijing.aliyuncs.com' + "/" + alikey
          })
        },
        fail: function(res) {
          console.log(res)
          wx.hideToast();
          wx.showModal({
            title: '错误提示',
            content: '上传图片失败',
            showCancel: false,
            success: function(res) {}
          })
        }
      });
    },
    // 获取oss
    queryOssData() {
      return new Promise((resolve, reject) => {
        console.log(resolve)
        api.getStsOss({}).then(res=>{
          // resolve(res)
          console.log(res)
        })
        // mWx.request({
        //   url: getStsOss,
        //   success: (res) => {
        //     resolve(res)
        //   }
        // })
      })
    },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindNameChange(e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindlevelChange(e) {
    this.setData({
      level: e.detail.value
    })
  },
  sureUp() {
    // var imgurl = this.data.imgUrls.split('?')
    console.log(this.data)
    if (this.data.imgUrls && this.data.name && this.data.date && this.data.level) {
      apis.userCertificationInsert({
        certificateImgSrc: this.data.imgUrls,
        certificationName: this.data.name,
        getTime: this.data.date,
        level: this.data.level
      }).then(res => {
        wx.showToast({
          title: '上传成功',
          icon: 'none'
        })
        wx.redirectTo({
          url: '/mine/pages/zhengshu/index',
        })
      })
    }else{
      wx.showToast({
        title: '请填写完整数据',
        icon:'none'
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})